package org.karelun.gef.dom;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.karelun.gef.editors.UserCaseDiagramEditor;
import org.karelun.gef.helper.DOMConstant;
import org.karelun.gef.model.ActorModel;
import org.karelun.gef.model.AssociationConnectionModel;
import org.karelun.gef.model.DependencyConnectionModel;
import org.karelun.gef.model.DiagramModel;
import org.karelun.gef.model.GeueralizationConnectionModel;
import org.karelun.gef.model.Node;
import org.karelun.gef.model.RealizeConnectionModel;
import org.karelun.gef.model.UserCaseModel;
import org.karelun.gef.utils.DOMUtils;
import org.w3c.dom.Element;

public class ReadUserCaseDiagram {
	private UserCaseDiagramEditor editor;
	private Map<String,Node> map=new HashMap<String,Node>();
	
	public ReadUserCaseDiagram(UserCaseDiagramEditor editor){
		this.editor=editor;
	}
	
	public void readXML(Element rootEle){
		DiagramModel diagram = editor.getDiagram();
		Element[] children = DOMUtils.getChildElements(rootEle);
		for(Element ele:children){
			String type = ele.getNodeName();
			if(DOMConstant.TAG_actor.equals(type)||DOMConstant.TAG_userCase.equals(type)){
				Node model=DOMConstant.TAG_actor.equals(type)?new ActorModel():new UserCaseModel();
//				ActorModel model = new ActorModel();
				String name = ele.getAttribute(DOMConstant.ATTR_name);
				model.setText(name);
				String id=ele.getAttribute(DOMConstant.ATTR_id);
				model.setId(id);
				map.put(id, model);
				
				Element locEle = DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_loc)[0];
				String x = locEle.getAttribute(DOMConstant.ATTR_x);
				String y = locEle.getAttribute(DOMConstant.ATTR_y);
				model.setLocation(new Point(Integer.parseInt(x),Integer.parseInt(y)));
				
				Element sizeEle = DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_size)[0];
				String height = sizeEle.getAttribute(DOMConstant.ATTR_height);
				String width = sizeEle.getAttribute(DOMConstant.ATTR_width);
				model.setSize(new Dimension(Integer.parseInt(width),Integer.parseInt(height)));
				
				diagram.addNode(model);
//			}else if(DOMConstant.TAG_userCase.equals(type)){
//				UserCase model = new UserCase();
//				String name = ele.getAttribute(DOMConstant.ATTR_name);
//				model.setName(name);
//				String id=ele.getAttribute(DOMConstant.ATTR_id);
//				model.setId(id);
//				map.put(id, model);
//				
//				Element locEle = DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_loc)[0];
//				String x = locEle.getAttribute(DOMConstant.ATTR_x);
//				String y = locEle.getAttribute(DOMConstant.ATTR_y);
//				model.setLocation(new Point(Integer.parseInt(x),Integer.parseInt(y)));
//				
//				Element sizeEle = DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_size)[0];
//				String height = sizeEle.getAttribute(DOMConstant.ATTR_height);
//				String width = sizeEle.getAttribute(DOMConstant.ATTR_width);
//				model.setSize(new Dimension(Integer.parseInt(width),Integer.parseInt(height)));
//				
//				diagram.addNode(model);
			}else if(DOMConstant.TAG_connection.equals(type)){
				Element sourceEle=DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_source)[0];
				String sourceNodeId=sourceEle.getAttribute(DOMConstant.ATTR_sourceNode);
				if(map==null||map.isEmpty())
					return;
				Node sourceNode=map.get(sourceNodeId);
				Element targetEle=DOMUtils.getChildElementsByTag(ele, DOMConstant.TAG_target)[0];
				String targetNodeId=targetEle.getAttribute(DOMConstant.ATTR_targetNode);
				Node targetNode=map.get(targetNodeId);
				
				String conn=ele.getAttribute(DOMConstant.ATTR_type);
				if(DOMConstant.assoc_connection.equals(conn)){
					AssociationConnectionModel connModel=new AssociationConnectionModel();
					connModel.setSource(sourceNode);
					connModel.setTarget(targetNode);
					sourceNode.addsourceConnection(connModel);
					targetNode.addtargetConnection(connModel);
				}else if(DOMConstant.depend_connection.equals(conn)){
					DependencyConnectionModel connModel=new DependencyConnectionModel();
					connModel.setSource(sourceNode);
					connModel.setTarget(targetNode);
					sourceNode.addsourceConnection(connModel);
					targetNode.addtargetConnection(connModel);
				}else if(DOMConstant.geuera_connection.equals(conn)){
					GeueralizationConnectionModel connModel=new GeueralizationConnectionModel();
					connModel.setSource(sourceNode);
					connModel.setTarget(targetNode);
					sourceNode.addsourceConnection(connModel);
					targetNode.addtargetConnection(connModel);
				}else if(DOMConstant.realize_connection.equals(conn)){
					RealizeConnectionModel connModel=new RealizeConnectionModel();
					connModel.setSource(sourceNode);
					connModel.setTarget(targetNode);
					sourceNode.addsourceConnection(connModel);
					targetNode.addtargetConnection(connModel);
				}
			}
		}
	}
}
